Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class Estabelecimento
    Inherits System.Web.UI.Page
    Dim blnGridSelectedRow As Boolean = False
    Private Sub CarregaGVW()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String = "SELECT CodigoDaHolding, CodigoDaCia, CodigoDoEstabelecimento, FilialDoEstabelecimento, RazaoSocialDoEstabelecimento FROM Estabelecimento ORDER BY CodigoDaHolding, CodigoDaCia, CodigoDoEstabelecimento"
            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Function VerificaDado(ByVal strConn As String) As Boolean

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String = "SELECT CodigoDaCia " & _
                     " FROM Estabelecimento " & _
                     " WHERE CodigoDaCia = " & HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text) & "'" & _
                     " GROUP BY CodigoDaHolding"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                If theSqlDataReader.Read() Then
                    VerificaDado = True
                End If
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                Console.WriteLine(ex.Message)
            End Try

        End Using

    End Function

    Private Function ExcluiEstabelecimento(ByVal strConn As String) As Boolean

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String
            theQueryString = "DELETE FROM Estabelecimento " & _
                     " WHERE CodigoDaCia = '" & HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text) & "'" & _
                     " AND CodigoDaHolding = '" & HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text) & "'"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString
            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        gvwData.PageIndex = e.NewPageIndex
        gvwData.DataBind()

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        'If Not Page.IsPostBack Then
        btnAlterar.Enabled = True
        btnExcluir.Enabled = True
        btnDadosFiscais.Enabled = True
        'End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        ' Header
        lblModulo.Text = "Administrador"
        lblModuloItem.Text = "Cadastro de Estabelecimento"

        btnAlterar.Enabled = False
        btnExcluir.Enabled = False
        btnDadosFiscais.Enabled = False

        CarregaGVW()

    End Sub

    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodigoDaHolding", "")
        Session.Add("strCodigoDaCia", "")
        Session.Add("strCodigoDoEstabelecimento", "")
        Session.Add("strFilialDoEstabelecimento", "")
        Session.Add("strRazaoSocialDoEstabelecimento", "")

        Response.Redirect("EstabelecimentoData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaHolding", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))
        Session.Add("strFilialDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        Session.Add("strRazaoSocialDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))

        Response.Redirect("EstabelecimentoData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Dim ConnectionTest As String
        ConnectionTest = "A"

        If (VerificaDado(ConnectionTest) = True) Then
            Exit Sub
        End If

        If (ExcluiEstabelecimento(ConnectionTest) = False) Then
            Exit Sub
        End If

        Response.Redirect("Estabelecimento.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub

    Protected Sub btnDadosFiscais_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDadosFiscais.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaHolding", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))
        Session.Add("strFilialDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        Session.Add("strRazaoSocialDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))

        Response.Redirect("EstabelecimentoDadosFiscais.aspx")

    End Sub

End Class
